﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;

namespace ChartingModel.Style
{
    public class PieChartStyle
    {

        //todo add code for multiselection and sinfgle selection
        
        
        ObservableCollection<Collections.PieChartDataList> dataList = new ObservableCollection<Collections.PieChartDataList>();

        Brush borderBrush = Brushes.Black;
        double borderThickness = 1.0;

        public ObservableCollection<Collections.PieChartDataList> DataList
        {
            get { return dataList; }
            private set{dataList = value;}
        }

        public Brush BorderColor
        {
            get { return borderBrush; }
            set { borderBrush = value; }
        }

        public void AddDataList(ObservableCollection<Collections.PieChartDataList> _dataList)
        {
            this.dataList = _dataList;
        }

        public void AddPie(double width, double height)
        {
            int nData = dataList.Count;

            double sum = 0.0;
            for (int i = 0; i < nData; i++)
            {
                sum += dataList[i].Item;
            }
            double startAngel = 0;
            double sweepAngel = 0;

            for (int i = 0; i < nData; i++)
            {
                int explode = dataList[i].Explode;
                if (sum <= 1)
                {
                    startAngel += sweepAngel;
                    sweepAngel = 2 * Math.PI * dataList[i].Item;
                }
                else if(sum > 1)
                {
                    startAngel += sweepAngel;
                    sweepAngel = 2 * Math.PI * dataList[i].Item / sum; 
                }

                double dx = explode * Math.Cos(startAngel + sweepAngel / 2);
                double dy = explode * Math.Sin(startAngel + sweepAngel / 2);
                if (Math.PI  > sweepAngel)
                {
                    this.DataList[i].MyPath = DrawArc(width, height, this.dataList[i].BrushColor, startAngel, startAngel + sweepAngel, dx, dy);
                }
                else
                {
                    var t = sweepAngel / 2;
                    var tfg = DrawArc(width, height, this.dataList[i].BrushColor, startAngel, startAngel + t, dx, dy);
                    var tttt = DrawArc(width, height, this.dataList[i].BrushColor,startAngel + t,startAngel + t + t, dx, dy);
                    DataList[i].MyPath  = new CombinedGeometry(GeometryCombineMode.Union, tfg, tttt);
                }
            }
        }

        Geometry DrawArc(double width, double height, Brush fillColor, double startAngel, double endAngel, double dx, double dy) 
        {
            Path path = new Path();
            path.Stroke = borderBrush;
  //          path.StrokeThickness = 50;
            path.Fill = fillColor;
            PathGeometry pg = new PathGeometry();
            PathFigure pf = new PathFigure();

            LineSegment ls1 = new LineSegment();
            LineSegment ls2 = new LineSegment();

            ArcSegment arc = new ArcSegment();
            double xc = width / 2 + dx;
            double yc = height / 2 + dy;
            double r = .8 * xc;

            pf.IsClosed = true;
            pf.StartPoint = new Point(xc, yc);
            pf.Segments.Add(ls1);
            pf.Segments.Add(arc);
            pf.Segments.Add(ls2);
            pg.Figures.Add(pf);
            path.Data = pg;

            ls1.Point = new Point(xc + r * Math.Cos(startAngel), yc + r * Math.Sin(startAngel));
            arc.SweepDirection = SweepDirection.Clockwise;
            arc.Point = new Point(xc + r * Math.Cos(endAngel), yc + r * Math.Sin(endAngel));
            if (r < 0)
            {
                return null;
            }

            arc.Size = new Size(r, r);
            ls2.Point = new Point(xc + r * Math.Cos(endAngel), yc + r * Math.Sin(endAngel));

            return path.Data;
        }

        public void RedrawPiece(double width, double height, int index)
        {


        }

    }
}
